using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace App
{
    public class Busy : PictureBox
    {
        Timer timer1;
        int tick = 0;
        public Busy()
        {
            timer1 = new Timer();
            timer1.Interval = 50;
            timer1.Tick += new EventHandler(timer1_Tick);
            this.Paint += new PaintEventHandler(Busy_Paint);
            this.Width = 32;
            this.Height = 32;
        }

        void Busy_Paint(object sender, PaintEventArgs e)
        {
            if (e.ClipRectangle.Width == 0 || e.ClipRectangle.Height == 0)
                return;
            Image img = Properties.Resources.Busy;
            Rectangle rectImg = new Rectangle(32 * tick, 0, 32, 32);
            e.Graphics.DrawImage(img, new Rectangle(0, 0, 32, 32), rectImg, GraphicsUnit.Pixel);
            img.Dispose();
        }

        void timer1_Tick(object sender, EventArgs e)
        {
            Invalidate();
            if (tick == 17)
            {
                tick = 0;
            }
            else
            {
                tick++;
            }
        }

        public bool IsBusy
        {
            get
            {
                return timer1.Enabled;
            }
            set
            {
                tick = 0;
                Visible = value;
                timer1.Enabled = value;
            }
        }
    }
}
